/**
 * @date 2018/10/31
 * @Description: 定时发送短信
 */
const schedule = require('node-schedule');
let moment = require('moment')
let sms = require("./sms");

var Order = require("../model/xjg/order");
var Notice = require("../model/xjg/notice");
var Occupy = require("../model/xjg/occupy");

// 标志当前是否可以遍历通知表发送短信
let flag = 0;

/* 定时器 */
function scheduleRecurrenceRule () {
  let rule = new schedule.RecurrenceRule();
  // rule.second = 0;
  rule.minute = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
  schedule.scheduleJob(rule, function () {
    let currentTime = moment().format('HH:mm')
    // 这段时间有可能出现需要发送短信的订单
    if ((parseInt(currentTime) >= 10 && parseInt(currentTime) <= 15) || (parseInt(currentTime) >= 16 && parseInt(currentTime) <= 21)) {
      flag = 1
    } else {
      flag = 0
    }
    if (flag) {
      if (!Notice.findAll) {
        return
      }
      Notice.findAll({
        where: {
          state: '0',
          send_time: {
            $lte: moment().format('YYYY-MM-DD HH:mm')
          }
          // send_time: moment().format('YYYY-MM-DD HH:mm')
        }
      }).then((data) => {
        // 短信通知即将就餐客户
        senMsg(data, -1)
        // 用餐通知管理员
        senMsg(data, 3)
        Notice.update({
          state: '1'
        }, {
          where: {
            send_time: {
              $lte: moment().format('YYYY-MM-DD HH:mm')
            }
          }
        })
      })
    }
    // 每天15点和22点更新订单为已完成
    switch (currentTime) {
      case '15:00':
        upDateOrder(1, Order);
        break;
      case '22:00':
        upDateOrder(2, Order);
        break;
      default:
        break;
    }
  });
}

/*自动更新订单*/
function upDateOrder (time, Order) {
  let where1 = {
    date: {
      $lt: moment().format('YYYY-MM-DD')
    },
    timeSlot: ['1', '2']
  }
  let where2 = {
    date: moment().format('YYYY-MM-DD'),
    timeSlot: [`${time}`]
  }
  // 小于今天的全部更新为3
  Order.update({
    state: '3' // 置为待评论
  }, {
    where: where1
  }).then((data) => {
    Occupy.destroy({
      where: where1
    }).then((r) => {
    })
  })
  // 今天的订单状态设置
  Order.update({
    state: '3' // 置为待评论
  }, {
    where: where2
  }).then((data) => {
    Occupy.destroy({
      where: where2
    }).then((r) => {
      Order.findAll({
        where: where2
      }).then((list) => {
        // 评论短信推送
        let _time = new Date(moment().format('YYYY-MM-DD HH:mm:ss'))
        senMsg(list, 1)
      })
    })
  })
}

module.exports = scheduleRecurrenceRule

function senMsg (data, type) {
  for (let i = 0; i < data.length; i++) {
    let senData = {
      name: data[i].userName,
      phone: data[i].phone,
      year: data[i].send_time ? data[i].send_time.split('-')[0] : '',
      month: data[i].send_time ? data[i].send_time.split('-')[1] : '',
      day: data[i].send_time ? data[i].send_time.split('-')[2] : '',
      time: data[i].send_time ? parseInt(data[i].send_time.split(':')[0]) + 1 : '',
      minute: data[i].send_time ? data[i].send_time.split(':')[1] : '',
      num: data[i].tableInfo || ''
    }
    // 评论通知
    switch (type) {
      case 1:
        senData.type = 1
        break
      // 用餐通知管理员
      case 3:
        senData.type = 3
        senData.phone = undefined
        break
      // 就餐通知
      default:
        senData.year = data[i].send_time.split('-')[0]
        senData.month = data[i].send_time.split('-')[1]
        senData.day = data[i].send_time.split('-')[2]
        senData.time = parseInt(data[i].send_time.split(':')[0]) + 1
        senData.minute = data[i].send_time.split(':')[1]
        senData.num = data[i].tableInfo || ''
        break
    }
    sms.sendMsg(senData)
  }
}